package com.vhly.tuite.twclient.util;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-4-30
 * Time: 18:09:10
 */
public class QuickArrayInputStream extends InputStream {

    private byte[] buf;
    private int index;
    private int count;
    private boolean eof;

    public QuickArrayInputStream(byte[] buf) {
        this.buf = buf;
        index = 0;
        if (buf == null || buf.length == 0) {
            eof = true;
            count = 0;
        } else {
            count = buf.length;
        }
    }

    public int read() throws IOException {
        int ret = -1;
        if (!eof) {
            if (index < count) {
                ret = buf[index++] & 0xff;

            } else {
                eof = true;
            }
        }
        return ret;
    }

    public void close() throws IOException {
        eof = true;
        count = 0;
        index = 0;
        buf = null;
    }
}